package com.ys.malini.util;

/**
 * 全角与半角 互转 工具类
 * 
 * @author Administrator
 * 
 */
public class CangleStrConver {

	// 半角转全角
	public static final String BQchange(String QJstr) {
		if (StringUtils.isEmpty(QJstr))
			return "";

		String outStr = "";
		String Tstr = "";
		byte[] b = null;

		for (int i = 0, len = QJstr.length(); i < len; i++) {
			try {
				Tstr = QJstr.substring(i, i + 1);
				b = Tstr.getBytes("unicode");
			} catch (java.io.UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (b[3] != -1) {
				b[2] = (byte) (b[2] - 32);
				b[3] = -1;
				try {
					outStr = outStr + new String(b, "unicode");
				} catch (java.io.UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			} else
				outStr = outStr + Tstr;
		}
		return outStr;
	}

	/*
	 * public static void main(String[] args) { String QJstr = "hello"; String
	 * QJstr1 = "ｈｅｈｅ";
	 * 
	 * String result = BQchange(QJstr); String result1 = QBchange(QJstr1);
	 * 
	 * System.out.println(QJstr + "\t" + result); System.out.println(QJstr1 +
	 * "\t" + result1); }
	 */

	// 全角转半角
	public static final String QBchange(String QJstr) {
		if (StringUtils.isEmpty(QJstr))
			return "";

		String outStr = "";
		String Tstr = "";
		byte[] b = null;

		for (int i = 0, len = QJstr.length(); i < len; i++) {
			try {
				Tstr = QJstr.substring(i, i + 1);
				b = Tstr.getBytes("unicode");
			} catch (java.io.UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (b[3] == -1) {
				b[2] = (byte) (b[2] + 32);
				b[3] = 0;
				try {
					outStr = outStr + new String(b, "unicode");
				} catch (java.io.UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			} else
				outStr = outStr + Tstr;
		}
		return outStr;
	}
}
